<?php

namespace App\Http\Controllers\Admin;

use App\Models\ResourceReadingListBook;
use App\Models\ResourceTotal;
use App\Validate\ResourceReadingListBookValidate;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * 数字资源阅读书单书籍表
 * Class Banner
 * @package app\api\controller
 */
class ResourceReadingListBookController extends CommonController
{
    public $model;
    public $validate = null;


    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceReadingListBook();
        $this->validate = new ResourceReadingListBookValidate();
    }



    /**
     * 获取相应的数据
     * @param except_reading_id 需要排除的书单id
     * @param resource_business_id 数字资源-第三方资源名id
     * @param big_type_id 系统大类
     * @param type_id 系统小类
     * @param keywords string 搜索关键词
     * @param page int 页码
     * @param limit int 分页大小
     */
    public function getReadingListBookData()
    {
        //增加验证场景进行验证
        // if (!$this->validate->scene('get_reading_list_book_data')->check($this->request->all())) {
        //     return $this->returnApi(201,  $this->validate->getError());
        // }
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? $this->request->limit : 10;
        $resource_business_id = $this->request->resource_business_id;
        $except_reading_id = $this->request->except_reading_id;
        $big_type_id = $this->request->big_type_id;
        $type_id = $this->request->type_id;
        //不允许选择视频
        if ($big_type_id == 3) {
            return $this->returnApi(203, '暂无数据');
        } elseif (empty($big_type_id)) {
            $big_type_id = [1, 2, 3, 4, 5, 999];
        }

        $resourceTotalModel = new ResourceTotal();
        $res = $resourceTotalModel->lists($this->request->keywords, $resource_business_id, $big_type_id, $type_id, null,null, $except_reading_id, null, null, null, null, $limit);
        if (!empty($res['data'])) {
            $res = $this->disPageData($res);
            $res['data'] = $this->disDataSameLevel($res['data'], 'con_big_type', ['type_name' => 'big_type_name']);
            $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);
            return $this->returnApi(200, "获取成功", true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }

    /**
     * 书单书籍列表
     * @param page int 页码
     * @param limit int 分页大小
     * @param reading_id 书单id
     * @param big_type_id 书籍大类
     * @param keywords string 搜索关键词
     */
    public function lists()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('lists')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $page = $this->request->page ? intval($this->request->page) : 1;
        $limit = $this->request->limit ? $this->request->limit : 10;
        $res = $this->model->lists($this->request->all());
        if (empty($res['data']) || empty($res)) {
            return $this->returnApi(203, "暂无数据");
        }
        $res = $this->disPageData($res);
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);

        return $this->returnApi(200, "查询成功", true, $res);
    }


    /**
     * 添加资源到书单中
     * @param reading_id int 书单id
     * @param resource_ids int 资源的id 多个逗号拼接
     */
    public function add()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $resource_ids = $this->request->resource_ids;
        $reading_id = $this->request->reading_id;
        DB::beginTransaction();
        try {
            $this->model->addResourceReadingListBook($reading_id, $resource_ids);

            DB::commit();
            return $this->returnApi(200, "添加成功", true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }

    /**
     * 删除（批量筛选）书单里的书籍
     * @param reading_id int 书单id
     * @param reading_list_resource_ids int 数据id 多个逗号拼接
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $reading_id = $this->request->reading_id;
        $res = $this->model->delResourceReadingListBook($reading_id, $this->request->reading_list_resource_ids);

        if ($res) {
            return $this->returnApi(200, "删除成功", true);
        }
        return $this->returnApi(202, '删除失败');
    }
}
